<template>
  <div class="manage">
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <!-- 用户的表单信息 -->
      <Form
        ref="form"
        :rules="rules"
        :inline="true"
        :model="form"
        label-width="80px"
      >
        <FormItem label="姓名" prop="name">
          <Input placeholder="请输入姓名" v-model="form.name"></Input>
        </FormItem>
        <FormItem label="年龄" prop="age">
          <Input placeholder="请输入年龄" v-model="form.age"></Input>
        </FormItem>
        <FormItem label="性别" prop="sex">
          <Select v-model="form.sex" placeholder="请选择性别">
            <Option label="男" :value="1"></Option>
            <Option label="女" :value="0"></Option>
          </Select>
        </FormItem>
        <FormItem label="出生日期" prop="birth">
          <DatePicker
            v-model="form.birth"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-DD"
          >
          </DatePicker>
        </FormItem>
        <FormItem label="地址" prop="addr">
          <Input placeholder="请输入地址" v-model="form.addr"></Input>
        </FormItem>
      </Form>
      <span slot="footer" class="dialog-footer">
        <Button @click="cancel">取 消</Button>
        <Button type="primary" @click="submit">确 定</Button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <Button @click="handleAdd" type="primary"> + 新增 </Button>
      <!-- form搜索区域 -->
      <Form :model="userForm" :inline="true">
        <FormItem>
          <Input placeholder="请输入名称" v-model="userForm.name"> </Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="onSubmit">查询 </Button>
        </FormItem>
      </Form>
    </div>
    <div class="common-table">
      <!-- Table表单 -->
      <Table stripe height="90%" :data="tableData" style="width: 100%">
        <TableColumn prop="name" label="姓名"> </TableColumn>
        <TableColumn prop="sex" label="性别">
          <template slot-scope="scope">
            <span>{{ scope.row.sex == 1 ? "男" : "女" }}</span>
          </template>
        </TableColumn>
        <TableColumn prop="age" label="年龄"> </TableColumn>
        <TableColumn prop="birth" label="出生日期"> </TableColumn>
        <TableColumn prop="addr" label="地址"> </TableColumn>
        <TableColumn label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </TableColumn>
      </Table>
      <div class="pager">
        <Pagination
          layout="prev, pager, next"
          :total="total"
          @current-change="handlePage"
        >
        </Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from "../api/index";
import {
  Form,
  FormItem,
  DatePicker,
  Option,
  Select,
  TableColumn,
  Table,
  Pagination,
  Input,
  Button,
} from "element-ui";
export default {
  name: "User",
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名" }],
        age: [{ required: true, message: "请输入年龄" }],
        sex: [{ required: true, message: "请输入性别" }],
        birth: [{ required: true, message: "请填写日期" }],
        addr: [{ required: true, message: "请填写地址" }],
      },
      tableData: [],
      modalType: 0, //0表示新增的弹窗，1表示编辑
      total: 0, //当前的总条数
      pageData: {
        page: 1,
        limit: 10,
      },
      userForm: {
        name: "",
      },
    };
  },
  components: {
    Form,
    FormItem,
    DatePicker,
    Option,
    Select,
    TableColumn,
    Table,
    Pagination,
    Button,
    Input,
  },
  methods: {
    // 提交用户表单
    submit() {
      this.$refs.form.validate((valid) => {
        // console.log(valid, 'valid')
        if (valid) {
          // 后续对表单数据的处理
          if (this.modalType === 0) {
            addUser(this.form).then(() => {
              // 重新获取列表的接口
              this.getList();
            });
          } else {
            editUser(this.form).then(() => {
              // 重新获取列表的接口
              this.getList();
            });
          }

          // 清空表单的数据
          this.$refs.form.resetFields();
          // 关闭弹窗
          this.dialogVisible = false;
        }
      });
    },
    //弹窗关闭的时候
    handleClose() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      this.modalType = 1;
      this.dialogVisible = true;
      //注意需要对当前行的数据进行深拷贝，否则会出错
      this.form = JSON.parse(JSON.stringify(row));
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //重新获取列表的接口
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.modalType = 0;
      this.dialogVisible = true;
    },
    //选择页码的回调函数
    handlePage(val) {
      console.log(val);
      this.pageData.page = val;
      this.getList();
    },
    // 获取列表的数据
    getList() {
      // 获取的列表的数据
      getUser({ params: { ...this.userForm, ...this.pageData } }).then(
        ({ data }) => {
          console.log(data);
          this.tableData = data.list;

          this.total = data.count || 0;
        }
      );
    },
    //列表的查询
    onSubmit() {
      this.getList();
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 90%;
  .manage-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .common-table {
    position: relative;
    height: calc(100% - 62px);
    .pager {
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }
}
</style>